package com.leoman.spinningmachine.base;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.leoman.helper.BaseAcHelper;
import com.leoman.helper.HelperLibrary;
import com.leoman.helper.util.ToastUtil;
import com.leoman.spinningmachine.R;
import com.leoman.spinningmachine.http.APICallback;
import com.leoman.spinningmachine.http.APIResponse;
import com.leoman.spinningmachine.login.LoginActivity;
import com.leoman.spinningmachine.util.DialogUtils;
import com.leoman.spinningmachine.util.SharedPreferencesUtils;
import com.umeng.analytics.MobclickAgent;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import cn.bingoogolapple.refreshlayout.BGARefreshLayout;

/**
 * Created by Administrator on 2017/6/16.
 * activity基类
 */
public abstract class BaseActivity extends BaseAcHelper implements View.OnClickListener, BGARefreshLayout.BGARefreshLayoutDelegate, APICallback.OnResponseListener {

    public RelativeLayout rl_title;
    public LinearLayout ll_back;
    public ImageView iv_right;
    public TextView tv_title, tv_right;
    private Unbinder unbinder;

    /**
     * bind layout resource file
     *
     * @return id of layout resource
     */
    protected abstract int getContentViewLayoutID();

    /**
     * init all views
     */
    protected abstract void initView(Bundle savedInstanceState);

    /**
     * init all events
     */
    protected abstract void initData();

    private boolean isFullscreen;

    public void setFullscreen(boolean isFullscreen) {
        this.isFullscreen = isFullscreen;
    }

    public void onResume() {
        super.onResume();
        //友盟统计
        MobclickAgent.onResume(this);
    }

    public void onPause() {
        super.onPause();
        //友盟统计
        MobclickAgent.onPause(this);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getContentViewLayoutID() != 0) {
            setContentView(getContentViewLayoutID());
        } else {
            throw new IllegalArgumentException("You must return a right contentView layout resource Id");
        }
        initView(savedInstanceState);
        setBarColor(isFullscreen, HelperLibrary.getInstance().getColor());
        initData();
    }

    @Override
    public void setContentView(int layoutResID) {
        super.setContentView(layoutResID);
        unbinder = ButterKnife.bind(this);
    }


    @Override
    protected void onDestroy() {
        if (unbinder != null)
            unbinder.unbind();
        super.onDestroy();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_back:
                CloseKeyboard();
                onBackPressed();
                break;
            default:
                break;
        }
    }

    //只有标题和返回
    public void initTitle(String str) {
        rl_title = (RelativeLayout) findViewById(R.id.rl_title);
        tv_title = (TextView) findViewById(R.id.tv_title);
        tv_title.setText(str);

        ll_back = (LinearLayout) findViewById(R.id.ll_back);
        ll_back.setOnClickListener(this);
    }

    //返回标题和右边文字
    public void initTitle(String str, String right) {
        rl_title = (RelativeLayout) findViewById(R.id.rl_title);
        tv_title = (TextView) findViewById(R.id.tv_title);
        tv_title.setText(str);

        ll_back = (LinearLayout) findViewById(R.id.ll_back);
        ll_back.setOnClickListener(this);

        tv_right = (TextView) findViewById(R.id.tv_right);
        tv_right.setVisibility(View.VISIBLE);
        tv_right.setText(right);
    }

    //返回标题和右边图片
    public void initTitle(String str, int image) {
        rl_title = (RelativeLayout) findViewById(R.id.rl_title);
        tv_title = (TextView) findViewById(R.id.tv_title);
        tv_title.setText(str);

        ll_back = (LinearLayout) findViewById(R.id.ll_back);
        ll_back.setOnClickListener(this);

        iv_right = (ImageView) findViewById(R.id.iv_right);
        iv_right.setVisibility(View.VISIBLE);
        iv_right.setImageResource(image);
    }

    // 获取验证码
    public void sentVetify(TextView textView) {
        TimeCount timeCount = new TimeCount(60000, 1000, textView);
        timeCount.start();
        textView.setAlpha(0.5f);
    }

    // 验证码倒计时
    public class TimeCount extends CountDownTimer {

        private TextView textView;

        public TimeCount(long millisInFuture, long countDownInterval, TextView textView) {
            super(millisInFuture, countDownInterval);
            this.textView = textView;
        }

        @Override
        public void onTick(long millisUntilFinished) {
            textView.setEnabled(false);
            textView.setText("重新获取\n(" + millisUntilFinished / 1000 + "秒)");
            textView.setTextSize(13);
        }

        @Override
        public void onFinish() {
            textView.setEnabled(true);
            textView.setText("获取验证码");
            textView.setAlpha(1f);
            textView.setTextSize(16);
        }
    }

    @Override
    public void onBGARefreshLayoutBeginRefreshing(BGARefreshLayout refreshLayout) {

    }

    @Override
    public boolean onBGARefreshLayoutBeginLoadingMore(BGARefreshLayout refreshLayout) {
        return false;
    }

    @Override
    public void OnFailureData(String error, Integer tag) {
        DialogUtils.getInstance().cancel();
        if (!TextUtils.isEmpty(error)) {
            ToastUtil.showToast(this, "网络错误", Toast.LENGTH_SHORT);
        }
    }

    @Override
    public void OnSuccessData(APIResponse apiResponse, Integer tag) {
        DialogUtils.getInstance().cancel();
    }

    @Override
    public void OnErrorData(String code, Integer tag) {
        DialogUtils.getInstance().cancel();
        if("100".equals(code)){
            SharedPreferencesUtils.getInstance().clear();
            ToastUtil.showToast(this,"登录已过期,请重新登录",Toast.LENGTH_SHORT);
            gotoOtherActivity(LoginActivity.class);
        }else
            ToastUtil.showToast(this, code, Toast.LENGTH_SHORT);
    }


}
